package com.yanqu.road.server.cmd.newmonopolymkt;

import com.yanqu.road.entity.systemgroup.newmonopolymarket.resgrab.NewMonopolyMarketReportData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.newmonopolymkt.NewMonopolyMarketModule;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.List;

@Cmd(code = GameProtocol.S_NEW_MONOPOLY_MARKET_GET_REPORT, desc = "查看 战报")
public class NewMonopolyMarketGetReportCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        NewMonopolyMarketProto.NewMonopolyMarketGetReportReqMsg req = NewMonopolyMarketProto.NewMonopolyMarketGetReportReqMsg.parseFrom(packet.getBytes());
        if(!NewMonopolyMarketMgr.canEnterSystem(player)){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        NewMonopolyMarketModule marketModule = player.getModule(NewMonopolyMarketModule.class);
        NewMonopolyMarketProto.NewMonopolyMarketGetReportRespMsg.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketGetReportRespMsg.newBuilder();
        switch (req.getType()){
            case 1:{
                List<NewMonopolyMarketReportData> reportDataList = marketModule.officeReportList;
                synchronized (reportDataList){
                    for(NewMonopolyMarketReportData item : reportDataList){
                        builder.addOfficeReportList(NewMonopolyMarketMgr.builderMonopolyMarketOfficeReportEntity(item));
                    }
                }
                marketModule.userData.setEnterOfficeReportLastTime(DateHelper.getCurrentSecond());
            }
            break;
            case 2:{
                List<NewMonopolyMarketReportData> reportDataList = marketModule.resGrabReportList;
                synchronized (reportDataList){
                    for(NewMonopolyMarketReportData item : reportDataList){
                        builder.addOfficeReportList(NewMonopolyMarketMgr.builderMonopolyMarketOfficeReportEntity(item));
                    }
                }
                marketModule.userData.setEnterResGrabReportLastTime(DateHelper.getCurrentSecond());
            }
            break;
            default:{
                ret = GameErrorCode.E_NEW_MONOPOLY_MARKET_OFFICE_TYPE_ERROR;
                return;
            }
        }
        marketModule.synRoleData();
        builder.setRet(0);
        player.sendPacket(ClientProtocol.U_NEW_MONOPOLY_MARKET_GET_REPORT, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            MonopolyMarketProto.MonopolyMarketGetReportRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketGetReportRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_NEW_MONOPOLY_MARKET_GET_REPORT, builder);
        }
    }

}
